$(function () {
    $("#datagrid").datagrid({
        title: "工资列表",
        fit: true,
        fitColumns: true,
        pagination: true,
        url: "/payRoll/query.do",
        columns: [[
            {field: 'id', title: '编号', width: 100},
            {field: 'employeeName', title: '员工名称', width: 100},
            {field: 'month', title: '月份', width: 100},
            {field: 'salary', title: '基础工资', width: 100},
            {field: 'workDay', title: '工作天数', width: 100},
            {field: 'afterNumber', title: '迟到天数', width: 100},
            {field: 'beforeNumber', title: '早退天数', width: 100},
            {field: 'upNumber', title: '补签天数', width: 100},
            {field: 'nowSalary', title: '最终工资', width: 100},
            {field: 'payTime', title: '结算时间', width: 100}
        ]],
        rownumbers: true,
        singleSelect: true,
        toolbar: "#toolbars"
    });
    $("#dialog").dialog({
        title: "增加/编辑",
        width: 340,
        height: 400,
        buttons: '#buttons',
        closed: true
    })
})

function edit() {
    var row = $("#datagrid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "您还没有选择要编辑的数据!", "info");
        return;
    }
    $("#dialog").dialog({
        closed: false,
        setTitle: '编辑'
    })
    $("#dialog").form("load", row);
}

function remove() {
    var row = $("#datagrid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "您还没有选择要编辑的数据!", "info");
        return;
    }
    $.messager.confirm("温馨提示", "您确定要删除该条数据吗?", function (r) {
        if (r) {
            $.get("/payRoll/remove.do", {id: row.id}, function (data) {
                if (data.success) {
                    $.messager.alert("温馨提示", "删除成功", "info");
                    $("#datagrid").datagrid("reload");
                } else {
                    $.messager.alert("温馨提示", data.msg, "info");
                }
            }, "json");
        }
    })
}

function reload() {
    $("#datagrid").datagrid("reload");
}

function submits() {
    $("#editForm").form('submit', {
        url: "/payRoll/saveOrUpdate.do",
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert("温馨提示", "提交成功", "info", function () {
                    $("#dialog").dialog("close", true);
                    $("#datagrid").datagrid("reload");
                })
            } else {
                $.messager.alert("温馨提示", data.msg, "info", function () {
                    $("#dialog").dialog("close", true);
                })
            }
        }
    })
}

//高级查询
function queryObject() {
    var employeeName = $("#employeeName").textbox("getValue");
    var beginDate = $("#beginDate").datebox("getValue");
    var endDate = $("#endDate").datebox("getValue");
    $("#datagrid").datagrid("load", {
        employeeName: employeeName,
        beginDate: beginDate,
        endDate: endDate
    })
}

//核算
function onState() {
    var row = $("#datagrid").datagrid("getSelected");
    if (!row) {
        $.messager.alert("温馨提示", "请选择要核算的数据!", "info");
    }
    $.get("/payRoll/onState.do", {id: row.id}, function (data) {
        if (data.success) {
            $.messager.alert("温馨提示", "审核成功", "info");
            $("#datagrid").datagrid("reload");
        } else {
            $.messager.alert("温馨提示", data.msg, "info");
        }
    }, "json");
}

//导出文件模板
function getImportXlsMode() {
    window.open("/payRoll/getImportXlsMode.do");
}

//上传excel表格
function importXls() {
    $("#importXls_form").form("submit", {
        url: "/payRoll/importXls.do",
        success: function (data) {
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert("温馨提示", data.msg, "info", function () {
                    $("#datagrid").datagrid("reload");
                });
            } else {
                $.messager.alert("温馨提示", "上传失败!" + data.msg, "info");
            }
        }
    })
}

function cancel() {
    $("#editForm").form("clear");
    $("#dialog").dialog("close");
}